package com.bincloud.vue.admin.common.util;

import java.util.regex.Pattern;

/**
 * @Description: 通用验证工具类
 * @Author: bincloud, mvpzhou
 * @Date: Create By bincloud, mvpzhou on 2022/2/19
 */
@SuppressWarnings("unused")
public class ValidateUtils {
    private ValidateUtils() {}

    public static boolean isEmail(String email) {
        String regex = "^\\w.+@\\w.*\\.\\w+$";
        return Pattern.matches(regex, email);
    }

    public static boolean isPhone(String phone) {
        String regex = "1[356789]\\d{9}$";
        return Pattern.matches(regex, phone);
    }

    public static boolean isIdCard(String idCard) {
        String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]";
        return Pattern.matches(regex, idCard);
    }

    public static boolean isChinese(String chinese) {
        String regex = "^[\u4E00-\u9FA5]+$";
        return Pattern.matches(regex, chinese);
    }

    public static boolean isEnglish(String english) {
        String regex = "^[a-zA-Z0-9]+$";
        return Pattern.matches(regex, english);
    }

    /**
     * 账号
     * 长度在5~16之间,字母或数字开头，允许字母,数字和下划线
     *
     * @param account 账号
     * @return 是否通过
     */
    public static boolean isAccount(String account) {
        String regex = "^[a-zA-Z0-9]\\w{4,15}$";
        return Pattern.matches(regex, account);
    }

    /**
     * 密码
     * 类型 1: 长度在8~16之间,只能包含字母,数字
     * 类型 2: 长度在8~16之间,只能包含字母,数字和下划线
     * 类型 3: 长度在8~16之间,只能包含字母,数字和下划线
     * 类型 4: 长度在8~16之间,只能包含字母,数字和下划线
     * 其他类型 : 长度在8~16之间,只能包含字母,数字和下划线
     *
     * @param password 密码
     * @param type 类型
     * @return 是否通过
     */
    public static boolean isPassword(String password, int type) {
        String regex;
        switch (type) {
            case 1:
                regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$";
                break;
            case 2:
                regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])\\w{8,16}$";
                break;
            case 3:
                regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[\\w!@#$%^&*()<>,.?/;:]{8,16}$";
                break;
            default: regex = "\\w{8,16}$";
        }
        return Pattern.matches(regex, password);
    }
}
